<?php

namespace app\api\controller;

use app\admin\model\Banner;
use app\api\model\ShopSearchLog;
use app\common\controller\Api;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 测试使用
     *
     */
    public function index()
    {
        $testData = db('shop_dianpu')->where('id', 1)->find();
        print_r(cdnurl($testData['images']));
        die;
        $this->success('请求成功');
    }

    public function shop_banner()
    {
        $this->success('banner列表', Banner::order('id desc')->select());
    }

    public function shop_list()
    {
        $post = $this->request->param();

        if (empty($post['lat']) || empty($post['lng'])) {
            $this->error('参数异常');
        }

        $where = [];
        if (isset($post['kilo']) && !empty($post['kilo'])) {
            $where['distance'] = ['<=', $post['kilo']];
        }
        if (isset($post['search']) && !empty($post['search'])) {
            $where['dianpu_name'] = ['like', '%' . $post['search'] . '%'];

            $searchModel = new ShopSearchLog();
            $searchLog = $searchModel->where('name', $post['search'])->find();
            if ($searchLog) {
                $searchLog->count += 1;
                $searchLog->save();
            } else {
                $searchModel->create([
                    'name' => $post['search'],
                ]);
            }
        }

        $data = model('app\api\model\wanlshop\Store')
            ->with('hotSaleGoods')
            ->where($where)
            ->field("id,dianpu_name,images,province,city,district,address,round(st_distance(point({$post['lng']},{$post['lat']}),point(lng,lat)),2) AS distance ")
            ->paginate()
            ->each(function ($item) {
                if (!empty($item['images'])) {
                    $item['images'] = cdnurl($item['images']);
                }
            });
        $this->success('门店列表', $data);
    }

    public function shop_hot_search_log()
    {
        $data = ShopSearchLog::order('count desc')->limit(6)->column('name');
        $this->success('店铺热搜', $data);
    }


    /**
     * 获取配置信息
     */
    public function getConfig()
    {
        if (!$this->request->isPost()) {
            $this->error('非法操作！');
        }
        $name = $this->request->post('name');
        $type = input('type', 'text');
        $data = config('site');
        if ($name) {
            $data = $data[$name];
            if ($type == 'img') {
                if (is_string($data)) {
                    $ext = pathinfo($data, PATHINFO_EXTENSION);
                    if (in_array($ext, ['png', 'jpg', 'jpeg', 'jif'])) {
                        $data = get_full_img($data);
                    }
                } else {
                    foreach ($data as &$val) {
                        $ext = pathinfo($val, PATHINFO_EXTENSION);
                        if (in_array($ext, ['png', 'jpg', 'jpeg', 'jif'])) {
                            $val = get_full_img($val);
                        }
                    }
                }
            }

            if (is_array($data)) {
                $ndata = [];
                foreach ($data as $k1 => $v1) {
                    $ndata[] = [
                        'id' => $k1,
                        'name' => $v1
                    ];
                }

                $data = $ndata;
            }
        }

        $this->success('配置信息', $data);
    }

}
